package com.demo.elasticjob.lite;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 数据流作业
 */
@Slf4j
@Component
public class ElasticJobLiteDataflowJob implements DataflowJob<Integer> {

    /**
     * 执行次数
     */
    private int number = 1;

    /**
     * 获取数据，然后执行 processData 方法。fetchData 方法的返回值为 null 或集合容量为空时，才停止抓取，否则作业将一直运行下去
     *
     * @param shardingContext 分片上下文
     * @return 数据
     */
    @Override
    public List<Integer> fetchData(ShardingContext shardingContext) {
        String jobParameter = shardingContext.getJobParameter();
        Integer value = Integer.valueOf(jobParameter);

        log.info("number:{}", number);
        // 当执行次数小于6时，返回数据
        if (number < 6) {
            number++;
            return List.of(value);
        } else {
            number++;
            return List.of();
        }
    }

    @Override
    public void processData(ShardingContext shardingContext, List<Integer> list) {
        log.info("list:{}", list);
    }
}
